<template>
  <el-dialog :title="title" :visible="showDialog" @close="close">
    <h2>部门表单内容</h2>
    <el-form ref="addDept" :model="form" :rules="rules" label-width="120px">
      <el-form-item prop="name" label="部门名称">
        <el-input v-model="form.name" placeholder="2-10个字符" style="width: 80%;" size="mini" />
      </el-form-item>

      <el-form-item prop="code" label="部门编码">
        <el-input v-model="form.code" placeholder="2-10个字符" style="width: 80%;" size="mini" />
      </el-form-item>

      <el-form-item prop="managerId" label="部门负责人">
        <el-select v-model="form.managerId" placeholder="请选择负责人" style="width: 80%;" size="mini">
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>

      <el-form-item prop="introduce" label="部门介绍">
        <el-input v-model="form.introduce" placeholder="1-100个字符" type="textarea" style="width: 80%;" :rows="4" size="mini" />
      </el-form-item>

      <el-form-item>
        <!-- 按钮 -->
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button size="mini" type="primary" @click="submit">确定</el-button>

            <el-button size="mini" @click="close">取消</el-button>

          </el-col>

        </el-row>

      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { addDepartment, getDepartment, getDepartmentDetail, getManagerList, updateDepartment } from '@/api/department'

export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      form: {
        name: '',
        code: '',
        managerId: '',
        introduce: ''
        // 父级部门的id
        // pid: ''
      },
      rules: {
        // 部门名称
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门名称的长度为2-10个字符', trigger: 'blur' },
          {
            trigger: 'blur',
            validator: async(rule, value, callback) => {
              // 1. 获取部门数据
              // 2. 判断输入的部门名称是否存在
              // 2.1 存在 -> 校验失败
              // 2.2 不存在-> 成功
              let res = await getDepartment()
              if (this.form.id) {
                // 编辑场景
                res = res.filter(item => item.id !== this.form.id)
              }
              // res.some / res.find2
              if (res.find(item => item.name === value)) {
                callback(new Error('部门名称已经存在了'))
              } else {
                callback()
              }
            }
          }
        ],
        // 部门编码
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门编码的长度为2-10个字符', trigger: 'blur' },
          {
            trigger: 'blur',
            validator: async(rule, value, callback) => {
              let res = await getDepartment()
              if (this.form.id) {
                // 编辑场景
                res = res.filter(item => item.id !== this.form.id)
              }
              if (res.find(item => item.code === value)) {
                callback(new Error('该编码已存在'))
              } else {
                callback()
              }
            }
          }
        ],
        // 部门介绍人
        managerId: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }], // 部门负责人id
        // 部门介绍
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 100, message: '部门介绍的长度为1-100个字符', trigger: 'blur' }
        ]
        // pid: '' // 父级部门的id 不需要做校验
      },
      managerList: []
    }
  },
  computed: {
    title() {
      return this.form.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    async submit() {
      // 1. 表单校验
      await this.$refs.addDept.validate()
      //   判断场景 编辑/新增
      if (this.form.id) {
        // 编辑
        await updateDepartment(this.form)
        this.$message.success('编辑部门成功')
      } else {
        // 新增
        await addDepartment({ ...this.form, pid: this.currentId })
        this.$message.success('新增部门成功')
      }
      // 3. 通知父组件更新
      this.$emit('updateDepartment')
      // 4. 重置表单 & 关闭弹窗
      this.close()
    },
    close() {
      this.form = {
        name: '',
        code: '',
        managerId: '',
        introduce: ''
      }
      this.$refs.addDept.resetFields()
      this.$emit('update:showDialog', false)
    },
    async getManagerList() {
      const res = await getManagerList()
      console.log(res)
      this.managerList = res
    },
    // 获取组织的详情
    async getDepartmentDetail() {
      this.form = await getDepartmentDetail(this.currentId)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
